#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
A module for finding scattering directions of a max on a givven density grid.
'''
import sys, os
from optparse import OptionParser
import fr_parser
from plane import Plane
from write_scatpairs_module import write_scatpair
from calculate_scatangle_module import calculate_scateringangle

optparser= OptionParser()   
optparser.add_option('-f', dest='filename',
					help='Prefics of a filenames with results')
optparser.add_option('-a', dest='scat_min_angle', default='45',
					help='Minium scat angle')
optparser.add_option('-k', dest='ishalf_angle', default='0',
					help='When this option is equal to 0 (whic is default) \
                          module will find all possible pairs directed appart.\
                          Else module will find only pairs directed in one half \
                          of a sphere, divided by plane thah perpendicular to\
                          both planes of pair. Except the planes wich has the\
                          scatering angle less than defined by this option.')
options, args = optparser.parse_args(sys.argv[1:])
ishalf_angle = int(options.ishalf_angle)
# Parsig of a maximums
lines = sys.stdin.read().split('\n')
maxs = []
for line in lines:
    val = fr_parser.parse_line(line)
    if val: maxs.append(val)    
pairs=[]
while len(maxs):
    m = maxs.pop(0)
    mplane = Plane(m[0], m[1])
    for d in maxs:
        dplane = Plane(d[0], d[1])
        if (mplane.dir == dplane.dir) and (mplane.dip == dplane.dip): continue
        isDirAppart, isOneHalf, mscatangle, dscatangle = \
                calculate_scateringangle(mplane, m[2], dplane, d[2])
        if not isDirAppart: continue
        if ishalf_angle != 0: # chek if planes is directed in different halfs
            if not isOneHalf:
                if mscatangle > ishalf_angle or dscatangle > ishalf_angle: continue
        # planes is directed appart
        if mscatangle>int(options.scat_min_angle) or\
                     dscatangle>int(options.scat_min_angle): continue
        scat_average = ( abs(mscatangle) + abs(dscatangle)) /2.0
        flag = -1
        for i in range(len(pairs)):
            pair = pairs[i]
            if mplane.dir == pair[0].dir and mplane.dip == pair[0].dip and \
            dplane.dir == pair[2].dir and dplane.dip == pair[2].dip: flag=i
            if mplane.dir == pair[2].dir and mplane.dip == pair[2].dip and \
            dplane.dir == pair[0].dir and dplane.dip == pair[0].dip: flag=i
        if flag ==  -1:  # these pair is not calculated yet
            pairs.append([mplane, m[2], dplane, d[2], scat_average])
            continue
        # now chek which of these pair has a smaller scatangle
        if pairs[flag][4] < scat_average: continue
        pairs[flag]=[mplane, m[2], dplane, d[2], scat_average]

# write pairs
# delete previously writed pairs if any
os.system("rm -f %s.*" % options.filename )
fnamesuffics = 0
for pair in pairs:
    mplane, mdir = pair[0], pair[1]       
    dplane, ddir = pair[2], pair[3]       
    write_scatpair(mplane, mdir, dplane, ddir, \
    "%s.%s" % ( options.filename, fnamesuffics) )
    fnamesuffics += 1
